package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvUnitDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 计量单位
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_INVUNIT)
public class InvUnitController extends SupporterController{
	private InvUnitDAO iuDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVUNIT_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.CN, Parm.DELETED, Parm.ON, Parm.TIME1, Parm.TIME2);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (iuDAO.findByMap(oc, map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVUNIT_LIST);
	}

	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			InvUnit iu = iuDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVUNIT, iu);
		}
		return new ModelAndView(Url.INVUNIT_EDIT);
	}
	
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvUnit iu, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (iu == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		
		RESULT result = RESULT.ERROR;
		if (iu.getCode() == null) {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvUnit> inv = iuDAO.findByNameOc(iu.getName(), oc);
			if (inv != null && inv.size() > 0)  {
				print(response, Msg.EXISTS);
				return;
			}
			iu.setOc(oc);
			iu.setCu(cu_code(request));
			iu.setCt(new Date());
			iu.setDeleted(false);
			result = iuDAO.add(iu);
		} else {
			result = iuDAO.update(iu);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	

	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvUnit iu = iuDAO.get(Integer.parseInt(code));
		if (null == iu) {
			print(response, Msg.NOT_FIND);
			return;
		}
		iu.setDeleted(true);
		if (iuDAO.upd(iu) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvUnit iu = iuDAO.get(Integer.parseInt(code));
		if (null == iu) {
			print(response, Msg.NOT_FIND);
			return;
		}
		iu.setDeleted(false);
		if (iuDAO.upd(iu) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setIuDAO(InvUnitDAO iuDAO) {
		this.iuDAO = iuDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
